#!/bin/bash
#
# Copyright (C) 2017 Red Hat Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

set -euo pipefail

. ${KOLA_EXT_DATA}/libtest.sh

set -x
cd $(mktemp -d)

case "${AUTOPKGTEST_REBOOT_MARK:-}" in
"")

if rpm -q foo 2>/dev/null; then
  fatal "found foo"
fi

# Disable repos, no Internet access should be required
rm -rf /etc/yum.repos.d/
booted_commit=$(rpm-ostree status --json | jq -r '.deployments[0].checksum')
ostree refs ${booted_commit} --create vmcheck
rpm-ostree rebase :vmcheck
ostree refs ${booted_commit} --create vmcheck_tmp/without_foo
rpm-ostree install ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm
rpmostree_assert_status '.deployments[0]["packages"]|length == 0' \
  '.deployments[0]["requested-packages"]|length == 0' \
  '.deployments[0]["requested-local-packages"]|length == 1' \
  '.deployments[0]["live-inprogress"]|not' \
  '.deployments[0]["live-replaced"]|not'
echo "ok install foo locally"
/tmp/autopkgtest-reboot "1"
;;
"1")
rpmostree_assert_status '.deployments[0]["packages"]|length == 0' \
  '.deployments[0]["requested-packages"]|length == 0' \
  '.deployments[0]["requested-local-packages"]|length == 1' \
  '.deployments[0]["live-inprogress"]|not' \
  '.deployments[0]["live-replaced"]|not'
assert_streq $(rpm -q foo) foo-1.2-3.x86_64
echo "ok pkg foo added locally"

booted_commit=$(rpm-ostree status --json | jq -r '.deployments[0].checksum')

ostree show --print-metadata-key=ostree.bootable ${booted_commit} >out.txt
assert_file_has_content_literal out.txt 'true'
echo "ok bootable metadata"

# check we could uninstall the package using either its NEVRA or name
rpm-ostree uninstall foo-1.2-3.x86_64
rpmostree_assert_status '.deployments[0]["requested-local-packages"]|length == 0'
rpm-ostree cleanup -p
rpm-ostree uninstall foo
rpmostree_assert_status '.deployments[0]["requested-local-packages"]|length == 0'
rpm-ostree cleanup -p
echo "ok uninstall by NEVRA or name"

# check that we can still request foo and it's dormant
rpm-ostree install foo

rpmostree_assert_status '.deployments[0]["packages"]|length == 0' \
 '.deployments[0]["requested-packages"]|length == 1' \
 '.deployments[0]["requested-local-packages"]|length == 1'
echo "ok request foo"

# check that uninstalling the local rpm makes us go back to repos
rpm-ostree uninstall foo-1.2-3.x86_64

rpmostree_assert_status '.deployments[0]["packages"]|length == 1' \
  '.deployments[0]["requested-packages"]|length == 1' \
  '.deployments[0]["requested-local-packages"]|length == 0'
echo "ok layer foo back from repos"

# check that trying to install a package already in the base errors out
ostree refs ${booted_commit} --create vmcheck_tmp/with_foo
ostree commit -b vmcheck --tree=ref=vmcheck_tmp/with_foo
rpm-ostree uninstall foo
rpm-ostree upgrade # upgrades to new base which has foo
if rpm-ostree install ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm; then
  assert_not_reached "didn't error out when trying to install same pkg"
fi
echo "ok error on layering same pkg in base"

# check that installing local RPMs without any repos available works
ostree commit -b vmcheck --tree=ref=vmcheck_tmp/without_foo
rpm-ostree upgrade
rpm-ostree install ${KOLA_EXT_DATA}/rpm-repos/0/packages/x86_64/foo-1.2-3.x86_64.rpm
echo "ok layer local foo without repos"
;;
*) echo "unexpected mark: ${AUTOPKGTEST_REBOOT_MARK}"; exit 1;;
esac
